package com.hs.mod_archive.helper

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.bumptech.glide.Glide
import com.hs.mod_archive.databinding.ItemOptLogBinding
import com.hs.mod_archive.view.MyListView
import com.hs.room.model.archive.OptLog


class OptLogManagerHelper(
    footPageListView: MyListView,
    var onItemClick: OnItemClickListener?,
    footSearchCallback: FootSearchCallback?
) :
    ListViewHelper(footPageListView, footSearchCallback) {
    private val mContext: Context = footPageListView.context
    private val viewHolders = arrayOfNulls<ItemOptLogBinding>(
        perPageCount
    )
    private val mParentView: LinearLayout = footPageListView.mViewList


    fun setListDataAndRefresh(datas: List<OptLog>?) {
        if (null == viewHolders[0]) {
            for (i in 0 until perPageCount) {
                val binding =
                    ItemOptLogBinding.inflate(LayoutInflater.from(mContext), mParentView, true)
                viewHolders[i] = binding
                if (isEvenOrOdd(i)) {
                    binding.root.setBackgroundColor(mColorBgOddNumberLine)
                }
            }
        }
        if (datas!!.isNotEmpty()) {
            mParentView.visibility = View.VISIBLE
            val newListSize = datas.size
            for (i in 0 until perPageCount) {
                if (i >= newListSize) {
                    val recordBinding = viewHolders[i]
                    recordBinding!!.root.visibility = View.INVISIBLE
                } else {
                    val h = viewHolders[i]
                    h!!.root.visibility = View.VISIBLE
                    val d = datas[i]
                    h.tvTime.text = d.time
                    h.tvUser.text = d.byName
                    h.tvType.text = d.what
                    h.tvLoginType.text = if (0 == d.loginType) {
                        "人脸认证"
                    } else if (1 == d.loginType) {
                        "指纹认证"
                    } else {
                        "密码认证"
                    }
                    if (!TextUtils.isEmpty(d.img)) {
                        Glide.with(mContext)
                            .load(d.img)
                            .into(h.ivScreenshot)
                    } else {
                        h.ivScreenshot.setImageBitmap(null)
                    }
                }
            }
        } else {
            mParentView.visibility = View.INVISIBLE
            for (binding in viewHolders) {
                binding!!.root.visibility = View.INVISIBLE
            }
        }
    }
}
